<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\api\library\ExceptionHandle;
use app\api\controller\Opencoupon;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $app_key = 'abSMX9VwVeIshOebQAH2Jro4T02uyHPB';
    protected $AppSecret = '9BmhWVbc51roVo1H';
    protected $url = 'http://quanyi.cn/index.php';
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {
        $this->success('请求成功');
    }

    public function ceshi(){
        //生成sign
        $params = $this->request->post();
        //
        $arr = [
            'channel' => $this->app_key,
            'timestamp' => time(),
            'goods_id' => $params['goods_id'],
            'mobile' => $params['mobile'],
            'exte_id' => $params['exte_id'],
        ];
        $arr['sign'] = $this->getSignV2($arr);
        return $result = ExceptionHandle::curl_json($this->url.'/api/Opencoupon/recharge',json_encode($arr));
    }

    public function ceshiGoods(){
        $arr = [
            'channel' => $this->app_key,
            'timestamp' => time(),
            /*'goods_id' => $params['goods_id'],
            'mobile' => $params['mobile'],*/
        ];
        $arr['sign'] = $this->getSignV2($arr);
        return $result = ExceptionHandle::curl_json($this->url.'/api/Opencoupon/internetCouponList',json_encode($arr));
    }

    public function ceshiSend(){
        $open = new Opencoupon();
        return $result = $open->send();
    }

    // public function yidong(){
    //      $data = [
    //         'appId'=> 100000059,
    //         'authType'=> 1,
    //         'businessId'=> "1295552911728300034",
    //         'effectType'=> "3",
    //         'effectTypeName'=> "立即生效",
    //         'extendParam'=> "%7B%22roomId%22%3A%22%22%2C%22shareUserId%22%3A%221351232218415415298%22%2C%22handleChannel%22%3A%221%22%7D",
    //         'handleChannel'=> "1",
    //         'handleMobile'=> "YJ0xbLiB6dX3T4JWIz4Fvw==",
    //         'isAuthentication'=> false,
    //         'randomCode'=> "151943",
    //         'showPlatform'=> 14,
    //         'userId'=> "1351232218415415298",
    //         'accesstoken'=>'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxMzYwMzExODI5MDEzNjAzMTExIiwiY3JlYXRlZCI6MTY1MTAzMjc3NDE0NiwiZXhwIjoxNjUxMDQ3MTc0fQ.osd2HdrKc2xBUZgThErrNIiWXkteMERdpWnmAzd54aI'
    //     ];
    //     $result = ExceptionHandle::curlPost('https://weidian.hb.vpclub.cn/cmhb/common/consumer/common/sendSMSOrVerifyBusiness?accesstoken='.$data['accesstoken'],$data);
    //     return $result;
    // }

    public function ceshiDetail(){
        $params = $this->request->post();
        $arr = [
            'channel' => $this->app_key,
            'timestamp' => time(),
            'goods_id' => $params['goods_id'],
            /*'mobile' => $params['mobile'],*/
        ];
        $arr['sign'] = $this->getSignV2($arr);
        return $result = ExceptionHandle::curl_json($this->url.'/api/Opencoupon/goodsDetail',json_encode($arr));
    }

    public function getSignV2($param)
    {
        //签名步骤一：按字典序排序参数
        ksort($param);
        $string = self::ToUrlParams($param); 
        // var_dump($string);
        //签名步骤二：在string后加入KEY
        $string = $string . "&app_secret=" . $this->AppSecret;
        //var_dump($string);
        //签名步骤三：MD5加密
        $string = md5($string);
        //var_dump($string);
        //签名步骤四：所有字符转为大写
        $result = strtoupper($string);
        // var_dump($result);
        return $result;
    }
    public function ToUrlParams($param)
    {
        $buff = "";
        foreach ($param as $k => $v) {
            if ($k != "sign" && $v != "" && !is_array($v)) {
                $buff .= $k . "=" . $v . "&";
            }
        }
        $buff = trim($buff, "&");
        return $buff;
    }
	public function createRandomStr($length){
	
		$str = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';//36个字符
		$strlen = 36;
		while($length > $strlen){
			$str .= $str;
			$strlen += 36;
		}
		$str = str_shuffle($str);
		return substr($str,0,$length);
	}
	
	public function shengcheng(){
		for($i = 0; $i < 60; $i++){
			$a = $this->createRandomStr(8);
			file_put_contents('1.txt', $a.PHP_EOL, FILE_APPEND);
			// halt($a);
		}
		
	}
}
